<template>
  <el-form label-width="60px" :rules="rules" :model="account" ref="registFormRef">
    <el-form-item label="用户名">
      <el-input v-model="account.username" />
    </el-form-item>
    <el-form-item label="密码">
      <el-input v-model="account.password" />
    </el-form-item>
  </el-form>
</template>

<script setup>
import { rules } from '../config/account-config'
import { reactive, ref } from 'vue'
import { register } from '@/api/user/index.js'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['registerSuccess'])

const registFormRef = ref()
const account = reactive({
  username: '',
  password: ''
})

function registCount() {
  registFormRef.value?.validate((valid) => {
    if (valid) {
      register(account).then(el => {
        if (el.code === 0) {
          ElMessage({
            message: '注册成功',
            type: 'success',
            duration: 5 * 1000
          })
          emit('registerSuccess', account)
        }
      })
    }
  })
}

defineExpose({ registCount })

</script>

<style scoped></style>
